/* CmdLineParser - Decompiled by JCavaj
 * Visit http://www.bysoft.se/sureshot/jcavaj/
 */
package jargs.gnu;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public class CmdLineParser
{
    private String[] remainingArgs = null;
    private Hashtable options = new Hashtable(10);
    private Hashtable values = new Hashtable(10);
    
    public abstract static class Option
    {
	private String shortForm = null;
	private String longForm = null;
	private boolean wantsValue = false;
	
	public static class StringOption extends Option
	{
	    public StringOption(char c, String string) {
		super(c, string, true);
	    }
	    
	    protected Object parseValue(String string) {
		return string;
	    }
	}
	
	public static class IntegerOption extends Option
	{
	    public IntegerOption(char c, String string) {
		super(c, string, true);
	    }
	    
	    protected Object parseValue(String string)
		throws IllegalOptionValueException {
		try {
		    return new Integer(string);
		} catch (NumberFormatException numberformatexception) {
		    throw new IllegalOptionValueException(this, string);
		}
	    }
	}
	
	public static class BooleanOption extends Option
	{
	    public BooleanOption(char c, String string) {
		super(c, string, false);
	    }
	}
	
	protected Option(char c, String string, boolean bool) {
	    if (string == null)
		throw new IllegalArgumentException
			  ("null arg forms not allowed");
	    shortForm = new String(new char[] { c });
	    longForm = string;
	    wantsValue = bool;
	}
	
	public Option(char c, String string, boolean b) {
		// TODO Auto-generated constructor stub
	}

	public Option(char c, String string, boolean b) {
		// TODO Auto-generated constructor stub
	}

	public Option(char c, String string, boolean b) {
		// TODO Auto-generated constructor stub
	}

	public Option(char c, String string, boolean b) {
		// TODO Auto-generated constructor stub
	}

	public String shortForm() {
	    return shortForm;
	}
	
	public String longForm() {
	    return longForm;
	}
	
	public boolean wantsValue() {
	    return wantsValue;
	}
	
	public final Object getValue(String string)
	    throws IllegalOptionValueException {
	    if (wantsValue) {
		if (string == null)
		    throw new IllegalOptionValueException(this, "");
		return parseValue(string);
	    }
	    return Boolean.TRUE;
	}
	
	protected Object parseValue(String string)
	    throws IllegalOptionValueException {
	    return null;
	}
    }
    
    public static class IllegalOptionValueException extends OptionException
    {
	/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
	private Option option;
	private String value;
	
	IllegalOptionValueException(CmdLineParser$Option option2, String string) {
	    super("illegal value '" + string + "' for option -"
		  + option2.shortForm() + "/--" + option2.longForm());
	    this.option = option2;
	    value = string;
	}
	
	public IllegalOptionValueException(CmdLineParser$Option option2, String string) {
		// TODO Auto-generated constructor stub
	}

	public Option getOption() {
	    return option;
	}
	
	public String getValue() {
	    return value;
	}
    }
    
    public static class UnknownOptionException extends OptionException
    {
	private String optionName = null;
	
	UnknownOptionException(String string) {
	    super("unknown option '" + string + "'");
	    optionName = string;
	}
	
	public String getOptionName() {
	    return optionName;
	}
    }
    
    public abstract static class OptionException extends Exception
    {
	OptionException(String string) {
	    super(string);
	}
    }
    
    public final void addOption(Option option) {
	options.put("-" + option.shortForm(), option);
	options.put("--" + option.longForm(), option);
    }
    
    public final Option addStringOption(char c, String string) {
	Option.StringOption stringoption = new Option.StringOption(c, string);
	addOption(stringoption);
	return stringoption;
    }
    
    public final Option addIntegerOption(char c, String string) {
	Option.IntegerOption integeroption
	    = new Option.IntegerOption(c, string);
	addOption(integeroption);
	return integeroption;
    }
    
    public final Option addBooleanOption(char c, String string) {
	Option.BooleanOption booleanoption
	    = new Option.BooleanOption(c, string);
	addOption(booleanoption);
	return booleanoption;
    }
    
    public final Object getOptionValue(Option option) {
	return values.get(option.longForm());
    }
    
    public final String[] getRemainingArgs() {
	return remainingArgs;
    }
    
    public void parse(String[] strings)
	throws IllegalOptionValueException, UnknownOptionException {
	Vector vector = new Vector();
	int i;
	for (i = 0; i < strings.length; i++) {
	    String string = strings[i];
	    if (!string.startsWith("-"))
		break;
	    if (string.equals("--")) {
		i++;
		break;
	    }
	    String string_0_ = null;
	    if (string.startsWith("--")) {
		int i_1_ = string.indexOf("=");
		if (i_1_ != -1) {
		    string_0_ = string.substring(i_1_ + 1);
		    string = string.substring(0, i_1_);
		}
	    }
	    Option option = (Option) options.get(string);
	    if (option == null)
		throw new UnknownOptionException(string);
	    Object object = null;
	    if (option.wantsValue()) {
		if (string_0_ == null) {
		    i++;
		    string_0_ = null;
		    if (i < strings.length)
			string_0_ = strings[i];
		}
		object = option.getValue(string_0_);
	    } else
		object = option.getValue(null);
	    values.put(option.longForm(), object);
	}
	for (/**/; i < strings.length; i++)
	    vector.addElement(strings[i]);
	remainingArgs = new String[vector.size()];
	int i_2_ = 0;
	Enumeration enumeration = vector.elements();
	while (enumeration.hasMoreElements()) {
	    remainingArgs[i_2_] = (String) enumeration.nextElement();
	    i_2_++;
	}
    }
}
